﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data.Quiz;
using UniMana.Utils.LoadData;
using UniMana.Utils.CusControls;
using System.Linq;
using DevExpress.XtraBars;

//huund.cntt@yahoo.com
//nguyen.huu@daihoclongan.edu.vn
namespace UniMana.GUI.Quiz
{
    public partial class FrmSubjectTopic : DevExpress.XtraEditors.XtraForm
    {
        public DBQuiz m_data = new DBQuiz();
        public bds m_bds = new bds();

        public FrmSubjectTopic()
        {
            InitializeComponent();
            LoadData();
            InitControlEvents();
        }

        public void LoadData()
        {
            gluTraining_Subject.GetData(m_data.m_DCReadOnly.Training_Subjects);
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(p=>p.SubjectManage==true));
            gctTopicList.DataSource = m_bds;
        }

        public void InitControlEvents()
        {
            xucBar1.btnAddNew_Click += new Controls.xucBar.ButtonClickHandler(this.btnAdd_Click);
            xucBar1.btnDelete_Click += new Controls.xucBar.ButtonClickHandler(this.btnDelete_Click);
            xucBar1.btnSave_Click += new Controls.xucBar.ButtonClickHandler(this.btnSave_Click);
        }

        private void gluTraining_Subject_EditValueChanged(object sender, EventArgs e)
        {
            int subjectKey = int.Parse(gluTraining_Subject.Properties.View.GetFocusedRowCellValue("Training_SubjectKey").ToString());
            m_bds.DataSource = m_data.m_DCSave.Quiz_SubjectTopics.Where(p => p.Training_SubjectKey == subjectKey);
        }

        private void btnAdd_Click(object sender, ItemClickEventArgs e)
        {
            Quiz_SubjectTopic stp = new Quiz_SubjectTopic();
            stp.Training_SubjectKey = int.Parse(gluTraining_Subject.Properties.View.GetFocusedRowCellValue("Training_SubjectKey").ToString());
            this.m_bds.Add(stp);
            this.m_bds.MoveLast();
        }

        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            this.m_bds.RemoveCurrent();
        }

        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {
            this.Validate();
            this.m_bds.EndEdit();
            this.m_data.m_DCSave.SubmitChanges();
        }

        private void FrmSubjectTopic_Load(object sender, EventArgs e)
        {

        }

    }
}